package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.ActivityPlanService;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.ActivityPlanDao;
import com.qdlc.p2p.dal.dto.ActivityPlan;
import com.qdlc.p2p.dal.model.ActivityPlanModel;

/**
 * 活动方案管理 service实现 - 提供增删改查 方法
 *
 * @author zlhx
 * @version 2.0
 * @date 2015-04-23 14:10
 */
@Service(value = "activityPlanService")
public class ActivityPlanServiceImpl implements ActivityPlanService {
    @Resource
    private ActivityPlanDao activityPlanDao;

    @Override
    public ActivityPlan save(ActivityPlan activityPlan) {
        return activityPlanDao.save(activityPlan);
    }

    @Override
    public int update(int status, int preStatus, long id) {
        return activityPlanDao.update(status, preStatus, id);
    }

    @Override
    public int update(ActivityPlanModel model) {
        return activityPlanDao.modify(model);
    }

    @Override
    public void delete(long id) {
        activityPlanDao.delete(id);
    }

    @Override
    public PageDataList<ActivityPlanModel> manageList(ActivityPlanModel model) {
        QueryParam param = QueryParam.getInstance();
        if (model != null) {
            //名字 检索
            if (!StringUtil.isBlank(model.getName())) {
                param.addParam("name", Operators.LIKE, model.getName());
            }

        }
        param.addPage(model.getPage(), model.getRows());
        //先查出 ActivityPlan 对象列表，之后遍历 为Model赋值，用于页面展示
        PageDataList<ActivityPlan> p = activityPlanDao.findAllPageList(param);
        List<ActivityPlanModel> list = new ArrayList<ActivityPlanModel>();
        PageDataList<ActivityPlanModel> plist = new PageDataList<ActivityPlanModel>();
        for (ActivityPlan activityPlan : p.getList()) {
            ActivityPlanModel activityPlanmodel = ActivityPlanModel.instance(activityPlan);
            //为活动方案中 红包、体验券、加息券 赋值中文
            if (activityPlan.getRateRule() != null)
                activityPlanmodel.setRateRuleName(activityPlan.getRateRule().getRuleName());

            if (activityPlan.getRedEnvelopeRule() != null)
                activityPlanmodel.setRedEnvelopeRuleName(activityPlan.getRedEnvelopeRule().getName());

            if (activityPlan.getExperienceRule() != null)
                activityPlanmodel.setExperienceRuleName(activityPlan.getExperienceRule().getName());
            list.add(activityPlanmodel);
        }
        plist.setPage(p.getPage());
        plist.setList(list);
        return plist;
    }


    @Override
    public ActivityPlan findById(long id) {
        return activityPlanDao.find(id);
    }
}
